package event;

import model.code.GlobalVariable;
import model.symbol.LocalVariable;
import model.symbol.StructureMember;
import model.symbol.Symbol;

/**
 * This is for and only for {@link GlobalVariable}, {@link LocalVariable} (including Parameters), and {@link StructureMember}.
 *
 * @author Neil Dickson
 */
public class SymbolTypeChangeEvent extends SymbolChangeEvent {
	private	String	oldType;
	private	String	newType;

	public SymbolTypeChangeEvent(Symbol symbol, String oldType, String newType) {
		super(symbol);
		if (!(symbol instanceof GlobalVariable) && !(symbol instanceof LocalVariable) && !(symbol instanceof StructureMember)) {
			throw new RuntimeException("\""+symbol.getName()+"\" is not a global or local variable");
		}
		this.oldType = oldType;
		this.newType = newType;
	}

	public Undoable undo() {
		if (symbol instanceof GlobalVariable) {
			((GlobalVariable)symbol).setType(oldType);
		}
		else if (symbol instanceof LocalVariable) {
			((LocalVariable)symbol).getType().replaceAll(oldType);
		}
		else { // symbol instanceof StructureMember
			((StructureMember)symbol).getType().replaceAll(oldType);
		}
		return new SymbolTypeChangeEvent(symbol,newType,oldType);
	}

	public String getDescription() {
		return "Change type of "+symbol.getName()+" from "+oldType+" to "+newType;
	}

	public String getOldType() {
		return oldType;
	}

	public String getNewType() {
		return newType;
	}
}
